﻿package com.wandian.view.mediator
{

	import com.greensock.TweenMax;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.InRoomPlayerDataProxy;
	import com.wandian.model.vo.PlayerVo;
	import com.wandian.view.components.ui.CardCount;
	import com.wandian.view.components.ui.CardManage;
	import com.wandian.view.components.ui.SoundManage;

	import flash.display.MovieClip;

	import org.osmf.logging.Log;
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class PublicCardMediator extends Mediator
	{
		private var _arrCard:Array;
		private var _arrCardXY:Array;
		private var _arrRecordCards:Array;
		private var _arrMyCard:Array;
		private var _global:GameData;
		private var _inRoomData:InRoomPlayerDataProxy;
		public static const NAME:String="PublicCardMediator";

		public function PublicCardMediator(param1:Object=null)
		{
			this._arrRecordCards=[];
			this._arrMyCard=[];
			this._global=GameData.getInstance();
			super(NAME, param1);
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			this.init();
			return;
		}

		private function init():void
		{
			this._arrCard=[];
			this._arrCardXY=[this.mcPublicCard.card1.y, this.mcPublicCard.card2.y, this.mcPublicCard.card3.y, this.mcPublicCard.card4.y, this.mcPublicCard.card5.y];
			var publicCards:Array=[this.mcPublicCard.card1, this.mcPublicCard.card2, this.mcPublicCard.card3, this.mcPublicCard.card4, this.mcPublicCard.card5];
			var index:int=0;
			while (index < publicCards.length)
			{

				this._arrCard[index]=new CardManage(publicCards[index], false);
				this._arrCard[index].visible=false;
				index=index + 1;
			}
			return;
		}

		/**
		 * 显示翻牌
		 * @param card1
		 * @param card2
		 * @param card3
		 */
		public function showThreeCard(card1:String, card2:String, card3:String):void
		{
			if (card1 == "-" || card2 == "-" || card3 == "-")
			{
				trace("=========翻牌,不对");
				return;
			}
			this._arrCard[0].visible=true;
			this._arrCard[1].visible=true;
			this._arrCard[2].visible=true;
			var _loc_4:*=this._arrCard[0].showCard(card1);
			var _loc_5:*=this._arrCard[1].showCard(card2);
			var _loc_6:*=this._arrCard[2].showCard(card3);
			SoundManage.playSound("ShowCard");
			SoundManage.playSound("ShowCard");
			SoundManage.playSound("ShowCard");
			this._arrRecordCards=[card1, card2, card3];
			var arrCards:Array=this._arrMyCard.concat(this._arrRecordCards);
			this.showCardTip(arrCards);
			return;
		}

		public function showFourCard(param1:String):void
		{
			if (param1 == "-")
			{
				return;
			}
			this._arrCard[3].visible=true;
			var _loc_2:*=this._arrCard[3].showCard(param1);
			this._arrRecordCards[3]=param1;
			SoundManage.playSound("ShowCard");
			var _loc_3:*=this._arrMyCard.concat(this._arrRecordCards);
			this.showCardTip(_loc_3);
			return;
		}

		public function showFiveCard(param1:String):void
		{
			if (param1 == "-")
			{
				return;
			}
			this._arrCard[4].visible=true;
			var _loc_2:*=this._arrCard[4].showCard(param1);
			this._arrRecordCards[4]=param1;
			SoundManage.playSound("ShowCard");
			var _loc_3:*=this._arrMyCard.concat(this._arrRecordCards);
			this.showCardTip(_loc_3);
			return;
		}

		/**
		 * 将坐位上玩家手中的5个牌与最佳牌比较，阴影小牌，高亮大牌
		 * @param seatId
		 * @param arrWinPlayer5Card
		 */
		private function selectedCard(seatId:int, arrWinPlayer5Card:Array):void
		{
			var recordIndex:int=0;
			if (arrWinPlayer5Card[0] == "-")
			{
				return;
			}
			this.initCard();
			var arrCopy5Card:*=arrWinPlayer5Card.concat();
			var arrSameCard:Array=[];
			var cardIndex:int=0;
			while (cardIndex < arrCopy5Card.length)
			{

				TweenMax.to(this._arrCard[cardIndex].card, 0.2, {colorMatrixFilter: {colorize: 0, amount: 0.5}});
				recordIndex=0;
				while (recordIndex < this._arrRecordCards.length)
				{

					if (arrCopy5Card[cardIndex] == this._arrRecordCards[recordIndex])
					{
						arrSameCard.push(recordIndex);
						break;
					}
					recordIndex=recordIndex + 1;
				}
				cardIndex=cardIndex + 1;
			}
			cardIndex=0;
			while (cardIndex < arrSameCard.length)
			{

				TweenMax.to(this._arrCard[arrSameCard[cardIndex]].card, 0.2, {y: this._arrCardXY[arrSameCard[cardIndex]] - 20, colorMatrixFilter: {}});
				cardIndex=cardIndex + 1;
			}
			return;
		}

		private function showCardTip(arrCards:Array):void
		{
			var playerVo:PlayerVo=this._inRoomData.getSitPlayerBySeatId(this._global.seatId);
			if (playerVo != null)
			{
				if (playerVo.state == GameData.PLAYING_GAME)
				{
					if (playerVo.lastAction != GameData.FOLD)
					{
						var cardType:Object=null;
						var arrCardType:Array=null;
						cardType=CardCount.instance().countCardType(arrCards);
						arrCardType=GameData.tip["cardType"].split("|");
						this.sendNotification(CLZNoticeName.SHOW_CARD_TIP, {seatId: playerVo.seatId, msg: arrCardType[cardType.type]});
					}
				}
			}


			return;
		}

		private function initCard():void
		{
			var _loc_1:int=0;
			while (_loc_1 < this._arrCard.length)
			{

				TweenMax.to(this._arrCard[_loc_1].card, 0.2, {y: this._arrCardXY[_loc_1], colorMatrixFilter: {}});
				_loc_1=_loc_1 + 1;
			}
			return;
		}

		public function hideAllPublicCard():void
		{
			this.initCard();
			var indx:int=0;
			while (indx < this._arrCard.length)
			{

				this._arrCard[indx].visible=false;
				indx=indx + 1;
			}
			return;
		}

		override public function onRegister():void
		{
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.START_GAME, CLZNoticeName.SNAPCARDS_FLOP, CLZNoticeName.SNAPCARDS_RIVER, CLZNoticeName.SNAPCARDS_TURN, CLZNoticeName.POTS, CLZNoticeName.GAME_OVER, CLZNoticeName.OUT_GAME_ROOM_REMOVE];
		}

		override public function handleNotification(notification:INotification):void
		{
			var arrCard:Object=null;
			var msg:String=null;
			var arrWinPlayer:Array=null;
			arrCard=notification.getBody();
			switch (notification.getName())
			{
				case CLZNoticeName.START_GAME:
				{
					this._arrMyCard=[arrCard.card1, arrCard.card2];
					break;
				}
			   //三次翻牌，将结果文字组合，并发给消息框
				case CLZNoticeName.SNAPCARDS_FLOP:
				{
					this.showThreeCard(arrCard.card1, arrCard.card2, arrCard.card3);
					//翻牌%VALUE%
					msg=GameData.replaceStrValue(GameData.tip["MSG_SNAPCARDS_FLOP"], "VALUE", GameData.getCardIcon(arrCard.card1) + "," + GameData.getCardIcon(arrCard.card2) + "," + GameData.getCardIcon(arrCard.card3));
					this.sendNotification(CLZNoticeName.CHAT_SYSTEM_TIP, msg);
					break;
				}
				case CLZNoticeName.SNAPCARDS_TURN: //转牌%VALUE%
				{
					this.showFourCard(arrCard.card1);
					msg=GameData.replaceStrValue(GameData.tip["MSG_SNAPCARDS_TURN"], "VALUE", GameData.getCardIcon(arrCard.card1));
					this.sendNotification(CLZNoticeName.CHAT_SYSTEM_TIP, msg);
					break;
				}
				case CLZNoticeName.SNAPCARDS_RIVER: //河牌%VALUE%
				{
					this.showFiveCard(arrCard.card1);
					msg=GameData.replaceStrValue(GameData.tip["MSG_SNAPCARDS_RIVER"], "VALUE", GameData.getCardIcon(arrCard.card1));
					this.sendNotification(CLZNoticeName.CHAT_SYSTEM_TIP, msg);
					break;
				}
				case CLZNoticeName.POTS:
				{
					arrWinPlayer=arrCard.arrWinPlayer;
					this.selectedCard(arrWinPlayer[0].seatId, [arrWinPlayer[0].card1, arrWinPlayer[0].card2, arrWinPlayer[0].card3, arrWinPlayer[0].card4, arrWinPlayer[0].card5]);
					break;
				}
				case CLZNoticeName.GAME_OVER:
				{
					this._arrMyCard=[];
					this.hideAllPublicCard();
					break;
				}
				case CLZNoticeName.OUT_GAME_ROOM_REMOVE:
				{
					this.removeThis();
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function removeThis():void
		{
			GameData.removeNumChildre(this.mcPublicCard, true);
			return;
		}

		private function get mcPublicCard():MovieClip
		{
			return viewComponent as MovieClip;
		}

	}
}
